package com.czy.owner.ui.userinfo;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.GrowthValueListAdapter;
import com.czy.owner.adapter.OrderMultiStyleAdapter2;
import com.czy.owner.api.GrowthValueDetailsListApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.GrowthValueDetailsList;
import com.czy.owner.entity.GrowthValueDetailsModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.easyrecycleview.decoration.DividerDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GrowthValueListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.easy_recylerview_list)
    EasyRecyclerView easyRecylerviewList;
    GrowthValueListAdapter growthValueListAdapter;
    private int page = 1;
    private int size = 20;
    private String session;
    private Handler handler = new Handler();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_growth_value_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        session = UserHelper.getInstance().getSessionInfoModel(this).getSession();
        easyRecylerviewList.setLayoutManager(new LinearLayoutManager(this));
        easyRecylerviewList.setAdapterWithProgress(growthValueListAdapter = new GrowthValueListAdapter(this));
        easyRecylerviewList.addItemDecoration(new DividerDecoration(this.getResources().getColor(R.color.appThemeBackgroundColor), PhoneUtils.dip2px(this, 0.5f)));
        View view = LayoutInflater.from(this).inflate(R.layout.order_empty_view, (ViewGroup) this.getWindow().getDecorView(), false);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText("您还没有相关记录");
        easyRecylerviewList.setEmptyView(view);
        easyRecylerviewList.setErrorView(R.layout.view_error_refresh);
        growthValueListAdapter.setMore(R.layout.view_more, this);
        growthValueListAdapter.setNoMore(R.layout.view_nomore);
        easyRecylerviewList.setRefreshListener(this);
        easyRecylerviewList.getSwipeToRefresh().setRefreshing(true);
        onRefresh();
    }

    @Override
    protected void updateViews() {
        toolbarTitle.setText(R.string.growth_value_list);
    }

    public void getDataList(final boolean isFrist) {
        GrowthValueDetailsListApi growthValueDetailsListApi = new GrowthValueDetailsListApi(new HttpOnNextListener<GrowthValueDetailsModel>() {
            @Override
            public void onNext(GrowthValueDetailsModel model) {


                if (model.getList().size() < size - 1) {
                    growthValueListAdapter.stopMore();
                }
                if (isFrist) {
                    growthValueListAdapter.clear();
                }
                growthValueListAdapter.addAll(model.getList());


            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                MyLog.e("yang", "--" + e.getMessage());
                growthValueListAdapter.pauseMore();
                easyRecylerviewList.getSwipeToRefresh().setRefreshing(false);

            }
        }, this);
        growthValueDetailsListApi.setSession(session);
        growthValueDetailsListApi.setPage(page + "");
        growthValueDetailsListApi.setSize(size + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(growthValueDetailsListApi);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(GrowthValueListActivity.this)) {
                    growthValueListAdapter.pauseMore();
                    easyRecylerviewList.showError();
                    return;
                }
                page++;
                getDataList(false);

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(GrowthValueListActivity.this)) {
                    growthValueListAdapter.pauseMore();
                    easyRecylerviewList.showError();
                    return;
                }
                page = 1;
                getDataList(true);
            }
        }, 1000);
    }

}
